#!/bin/bash

printUsage()
{
	echo "makeSchema [-oudpfh]"
	echo "o: adresse du serveur (localhost)"
	echo "u: utilisateur (root)"
	echo "d: nom de la base de données (coMexpert_test)"
	echo "p: demande le mot de passe"
	echo "h: affiche l'aide et quitte"
}

host="localhost"
user="root"
mdp=""
base="coMexpert_test"

while getopts o:u:d:ph name
do
	case $name in
		o)
			host="$OPTARG"
			;;
		u)
			user="$OPTARG"
			;;
		d)
			base="$OPTARG"
			;;
		p)
			read -s -p "Mot de passe: " mdp
			mdp="--password="$mdp
			;;
		h)
			printUsage
			exit 0
			;;
	esac
done

echo !!!! ATTENTION FAUT FAIRE CA EN  ROOT !!!!
mysqldump -u $user -h $host $mdp -d --default-character-set=utf8 --compact $base | sed 's/AUTO_INCREMENT=[0-9][0-9]*/AUTO_INCREMENT=1/g' > coMexpert.sql
mysql -h $host -u $user $mdp $base --execute "ALTER TABLE \`schema\` ORDER BY \`table\`, champ"
mysqldump -u $user -h $host $mdp -t --default-character-set=utf8 --compact $base schema > schema.sql
mysqldump -u $user -h $host $mdp -t --default-character-set=utf8 --compact $base rechercheLier > rechercheLier.sql
mysqldump -u $user -h $host $mdp -t --default-character-set=utf8 --compact $base version > version.sql
mysqldump -u $user -h $host $mdp -d -t -R --default-character-set=utf8 --compact $base > procedure.sql
mysqldump -u $user -h $host $mdp -d -t --default-character-set=utf8 --triggers --compact $base > triggers.sql

rm -rf coMexpert.tar.gz
tar -cvf coMexpert.tar coMexpert.sql schema.sql rechercheLier.sql procedure.sql version.sql triggers.sql
rm coMexpert.sql schema.sql rechercheLier.sql procedure.sql version.sql triggers.sql
gzip coMexpert.tar
